package logic

import (
	"context"

	"server_1.0/service/class/class"
	"server_1.0/service/class/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryLogic {
	return &QueryLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *QueryLogic) Query(in *class.Empty) (*class.QReps, error) {
	usersHandle := make([]*class.Reps, 0)
	count := 0
	pnos, _ := l.svcCtx.ClassModel.GetAll(l.ctx)

	for _, item := range pnos {
		usersHandle = append(
			usersHandle,
			&class.Reps{
				No:   item.Cid,
				ID:   item.Cmno,
				Name: item.Cname,
			})
		count += 1
	}
	return &class.QReps{
		Count: int64(count),
		QRs:   usersHandle,
	}, nil
}
